package com.examlpe.lotterysystem.controller.handler;

import com.examlpe.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.examlpe.lotterysystem.common.exception.ControllerException;
import com.examlpe.lotterysystem.common.exception.ServiceException;
import com.examlpe.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice //捕获全局异常
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> ServiceException(ServiceException e) {
        //打印错误日志
        log.error("ServiceException",e);
        //构造错误返回结果
       return CommonResult.error(
               GlobalErrorCodeConstants.INTERNAL_SERVER_ERROP.getCode(),
               e.getMassage());
    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> ControllerException(ControllerException e) {
        //打印错误日志
        log.error("ControllerException",e);
        //构造错误返回结果
        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROP.getCode(),
                e.getMassage());
    }
    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> Exception(Exception e) {
        //打印错误日志
        log.error("服务异常",e);
        //构造返回结果
        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROP.getCode(),
                e.getMessage());
    }
}
